const mongoose = require("mongoose")

const { Schema, model } = mongoose

const TopicSchema = new Schema({
  __v: { select: false, type: Number },
  name: { type: String, required: true, maxlength: 100 },
  avatar_url: { type: String },
  introduction: { type: String, select: false },
  meta: {
    type: {
      createdAt: {
        type: Date,
        default: Date.now()
      },
      updatedAt: {
        type: Date,
        default: Date.now()
      }
    },
    select: false,
    default: {}
  }
})

TopicSchema.pre("save", function(next) {
  this.isNew
    ? (this.meta.createdAt = this.meta.updatedAt = Date.now())
    : (this.meta.updatedAt = Date.now())
  next()
})

const Topic = new model("Topic", TopicSchema, "Topic")

module.exports = Topic
